"ui";
// 安卓库
importClass(android.graphics.Color);
importClass(android.graphics.drawable.GradientDrawable);
// 导入库
var project = require('./project.json');
var storage = storages.create("autotouchShijieXy");
// 固定数据
var colorHei = "#000000"
var hong = "#E63F31";
var floatyWindow; // 悬浮窗
var myScriptEngine;
var jietuquanxian = false; // 重写打印

// 缓存数据
var picJizhun = 1;
var picJizhunHeight = 1;
var deviceWidth = device.width;
var deviceHeight = device.height;
log('宽高：', deviceWidth, deviceHeight)

var ziyuanZuobianWeizhi = storage.get("ziyuanZuobianWeizhi", { x: 0, y: 0, w: 300, h: 65 }); // 资源信息坐标
var toCaijiList = storage.get("caijiItems", [{ x: "66", y: "66", level: "1", type: "1" }, { x: "88", y: "88", level: "1", type: "2" }, { x: "100", y: "100", level: "1", type: "3" }]); // 资源列表 坐标XY 等级 类型 粮木矿-123


ui.layout(
    <drawer id="drawer" >
        <vertical>
            <appbar>
                <toolbar id="toolbar" title="世界模式 挂机辅助" />
                <tabs id="tabs" />
            </appbar>

            <viewpager id="viewpager">
                <ScrollView >
                    <frame>
                        <vertical padding="10sp" >
                            <horizontal>
                                <text text="公告：" textColor={hong} textSize="15sp" />
                                <text text="模拟器上出现进世界模式后闪退,请提高模拟器CPU核心数 " id="gonggaoa" textColor={hong} textSize="14sp" />
                            </horizontal>
                            <text text="蓝块区域为新开放功能谨慎使用，红块暂不可用逐步开放" gravity="left" marginTop="6sp" />
                            {/* 主流程 */}
                            <horizontal marginTop="10sp" padding="8sp" bg="#99FE01">
                                <checkbox id="tianxuan" text="天选" />
                                <checkbox id="junbao" text="领取军报" />
                                <checkbox id="lingqurenwu" text="领取任务" />
                                <checkbox id="zhengshou" text="天选征收" />
                            </horizontal>
                            {/* 自动打野 */}
                            <vertical marginTop="8sp" padding="8sp" bg="#FFFF01">
                                <horizontal >
                                    <text text=" 是否开启：" textColor={colorHei} textSize="15sp" />
                                    <checkbox id="isdaye" text="自动打野(默认全刷)" />
                                </horizontal>
                                <horizontal marginTop="2sp">
                                    <checkbox id="ye1" text="1" />
                                    <checkbox id="ye2" text="2" />
                                    <checkbox id="ye3" text="3" />
                                    <checkbox id="ye4" text="4" />
                                    <checkbox id="ye5" text="5" />
                                    <checkbox id="ye6" text="6" />
                                    <checkbox id="ye7" text="7" />
                                </horizontal>
                                <horizontal marginTop="2sp">
                                    <text text=" 保留前" textColor={colorHei} textSize="14sp" />
                                    <input id="shuayebaoliu" text="0" textSize="16sp" inputType="number" padding="8 6" />
                                    <text text="个英雄不参与打野，用来备战和采集" textColor={colorHei} textSize="14sp" />
                                </horizontal>
                                <vertical>
                                    <horizontal>
                                        <checkbox id="fangqiangye" textColor={colorHei} text="防止抢野怪" />
                                        <checkbox id="chihouzhencha" textColor={colorHei} text="斥候侦查" />
                                    </horizontal>
                                    <horizontal>
                                        <checkbox id="shuayewushang" textColor={colorHei} text="忽略出兵战力不保留英雄(提升刷野速度)" />
                                    </horizontal>
                                    <horizontal>
                                        <text text=" 刷野出兵战力：" textColor={colorHei} textSize="15sp" />
                                        <spinner id="shuayezhanli" entries="胜券在握|优势明显|我方稍强|胜负各半" spinnerModer="dialog" />
                                    </horizontal>
                                    <horizontal>
                                        <checkbox id="centerZhucheng" />
                                        <text text="主城为中心" textColor={colorHei} textSize="15sp" />
                                        <input text="1" id="centerNPing" textSize="16sp" inputType="number" padding="8 6" />
                                        <text text="屏内范围" textColor={colorHei} textSize="15sp" />
                                    </horizontal>
                                    <horizontal>
                                        <checkbox id="caijiquyuDy" />
                                        <text text="采集坐标区域打野" textColor={colorHei} textSize="15sp" />
                                    </horizontal>
                                    <horizontal>
                                        <checkbox id="zuoshang" text="左上联盟" />
                                        <checkbox id="zuozhong" text="左中联盟" />
                                        <checkbox id="zuoxia" text="左下联盟" />
                                    </horizontal>
                                    <horizontal>
                                        <checkbox id="youshang" text="右上联盟" />
                                        <checkbox id="youzhong" text="右中联盟" />
                                        <checkbox id="youxia" text="右下联盟" />
                                    </horizontal>
                                    <horizontal>
                                        <checkbox id="changfangxingDy" />
                                        <text text="坐标" textColor={colorHei} textSize="15sp" />
                                        <input text="88,88,168,168" id="fangxingPing" textSize="16sp" inputType="number" padding="8 6" />
                                        <text text="内范围" textColor={colorHei} textSize="15sp" />
                                    </horizontal>
                                </vertical>
                            </vertical>
                            {/* 功能篇 */}
                            <vertical marginTop="8sp" padding="8sp" bg="#E43C2F">
                                <horizontal marginTop="2sp">
                                    <checkbox id="lingtai" text="灵台直接退" />
                                    <checkbox id="fumo" text="降妖伏魔(跳)" />
                                    <checkbox id="leitai" text="擂台(跳)" />
                                </horizontal>
                            </vertical>
                            {/* 三国战纪 */}
                            <horizontal marginTop="10sp" padding="8sp" bg="#99FE01">
                                <checkbox id="isSanguo" text="三国战纪" />
                            </horizontal>

                            {/* 其他设置 */}
                            <vertical marginTop="8sp" padding="8sp" bg="#FFC801">
                                <horizontal marginTop="2sp">
                                    <text text=" 是否检测登陆：" textColor={colorHei} textSize="15sp" />
                                    <checkbox id="jaincedenglu" text="检测登陆" />
                                </horizontal>
                                <horizontal>
                                    <text text=" 登录方式：" textColor={colorHei} textSize="15sp" />
                                    <spinner id="denglufangshi" entries="QQ登陆|微信" spinnerModer="dialog" />
                                </horizontal>
                                <horizontal >
                                    <checkbox id="otherQqLogin" text="其他QQ号登陆" />
                                    <input id="otherQqAccount" hint="请输入已登陆其他QQ号" text="" textSize="16sp" inputType="text" gravity="left" w="*" marginLeft="2sp" marginRight="18sp" />
                                </horizontal>
                                <horizontal>
                                    <text text=" 任务完成后：" textColor={colorHei} textSize="15sp" />
                                    <spinner id="yunxingfangshi" entries="智能运行|循环运行|定时运行" spinnerModer="dialog" />
                                </horizontal>
                                <horizontal marginTop="2sp">
                                    <checkbox id="jinchengshouhu" text="循环运行进程守护" />
                                </horizontal>
                                <horizontal>
                                    <text text=" 定时运行-每隔" textColor={colorHei} textSize="14sp" />
                                    <input id="intermission" text="0" textSize="16sp" inputType="number" padding="8 6" />
                                    <text text="小时自动运行" textColor={colorHei} textSize="14sp" />
                                </horizontal>
                                <horizontal>
                                    <checkbox id="islingdidenglu" text="另地登陆重连等待" />
                                    <input text="18" id="lingdidenglu" textSize="16sp" inputType="number" padding="8 6" />
                                    <text text="分钟" textColor={colorHei} textSize="15sp" />
                                </horizontal>
                            </vertical>

                        </vertical>
                    </frame>
                </ScrollView>
                {/* 采集设置 */}
                <ScrollView>
                    <frame>
                        <vertical padding="10sp">
                            <vertical bg="#99FE01" padding="8sp">
                                <horizontal >
                                    <text text=" 是否开启：" textColor={colorHei} textSize="15sp" />
                                    <checkbox id="caijiziyuan" text="资源采集" />
                                </horizontal>
                                <horizontal marginTop="1sp">
                                    <text text=" 附近" textColor={colorHei} textSize="14sp" />
                                    <input id="beixuanziyuanFw" text="2" textSize="16sp" inputType="number" padding="8 6" />
                                    <text text="屏之内" textColor={colorHei} textSize="14sp" />
                                </horizontal>
                                <checkbox id="beixuanziyuan" text="坐标资源不能采集时采集附近资源" />
                                <horizontal marginTop="1sp" bg="#2E7CEE">
                                    <checkbox id="bucaijishiduan" text="不采集坐标资源:" />
                                    <input id="bucaijishiduan1" text="1" textSize="16sp" inputType="number" padding="8 6" />
                                    <text text="时 - " textColor={colorHei} textSize="14sp" />
                                    <input id="bucaijishiduan2" text="5" textSize="16sp" inputType="number" padding="8 6" />
                                    <text text="时" textColor={colorHei} textSize="14sp" />
                                </horizontal>
                                <checkbox id="zhicaifujin" text="不采集坐标资源" />
                                
                                <checkbox id="baohanziji" text="可采集自己领地资源" />
                                <horizontal marginTop="6sp">
                                    <text text=" 保留前" textColor={colorHei} textSize="14sp" />
                                    <input id="caijibaoliuHero" text="0" textSize="16sp" inputType="number" padding="8 6" />
                                    <text text="个英雄不参与采集,强制读分时" textColor={colorHei} textSize="14sp" />
                                </horizontal>
                                <horizontal marginTop="6sp">
                                    <text text=" 采集资源存量不少于" textColor={colorHei} textSize="14sp" />
                                    <input id="cunliangNum" text="19000" textSize="16sp" inputType="number" padding="8 6" />
                                    <text text="的资源" textColor={colorHei} textSize="14sp" />
                                </horizontal>
                                <horizontal marginTop="6sp">
                                    <text text=" 存量少于" textColor={colorHei} textSize="14sp" />
                                    <input id="cunliangNumSy" text="0" textSize="16sp" inputType="number" padding="8 6" />
                                    <text text="自动撤回" textColor={colorHei} textSize="14sp" />
                                </horizontal>
                                <horizontal>
                                    <text text=" 采集优先：" textColor={colorHei} textSize="15sp" />
                                    <spinner id="caijiyouxian" entries="采集速度优先|采集负重优先" spinnerModer="dialog" />
                                </horizontal>
                                <checkbox id="qiangzhidufen" text="采集优先-强制读分(不读采集标志)" />
                                <checkbox id="quziyuan" text="仓库取资源(读取存量升级练兵等)" />
                                <checkbox id="ziyuancunchu" text="资源存仓库" />
                                <checkbox id="dongtaizyyouxian" text="根据存量自动调整采集列表顺序" />
                            </vertical>
                            <text text="右下角“+”添加资源后请点击保存" textColor={colorHei} textSize="12sp" marginTop="8sp" />
                            <horizontal>
                                <button w='55' textSize='10sp' id="ziyuanClean">清空</button>
                                <button w='55' textSize='10sp' id="zyDaochu">导出</button>
                                <button w='55' textSize='10sp' id="zyDaoru">导入</button>
                                <button w='55' style="Widget.AppCompat.Button.Colored" textSize='10sp' id="ziyuanSave">保存</button>
                            </horizontal>
                            <horizontal>
                                <button w='55' textSize='10sp' id="dengjiPaixu">等级↓</button>
                                <button w='55' textSize='10sp' id="dengjiPaixu0">粮↓</button>
                                <button w='55' textSize='10sp' id="dengjiPaixu1">木↓</button>
                                <button w='55' textSize='10sp' id="dengjiPaixu2">矿↓</button>
                            </horizontal>

                            <list id="ziyuanList" w='*' h='200sp'>
                                <horizontal>
                                    <button w='45' textSize='9sp' id="delete">删除</button>
                                    <text text="X:" textColor={colorHei} textSize="14sp" marginLeft="6" />
                                    <text w='48' id="ziyuanX" textColor={colorHei} text="{{this.x}}" textSize="16sp" gravity="center" padding="5 12" />
                                    <text text="Y:" textColor={colorHei} textSize="14sp" />
                                    <text w='48' id="ziyuanY" textColor={colorHei} text="{{this.y}}" textSize="16sp" gravity="center" padding="5 12" />
                                    <input w='25' text="{{this.level}}" textSize="16sp" inputType="number" padding="6 12" marginLeft="6" />
                                    <text text="级" textColor={colorHei} textSize="14sp" />
                                    <text w='28' text="{{ziyuanGuolvFunc(this.type)}}" textColor={colorHei} textSize="16sp" padding="6 12" marginLeft="6" />
                                </horizontal>
                            </list>
                            <fab id="ziyuanAdd" w="auto" h="auto" src="@drawable/ic_add_black_48dp" margin="16" layout_gravity="bottom|right" tint="#ffffff" />
                        </vertical>
                    </frame>
                </ScrollView>
                {/* 升级备战 */}
                <ScrollView>
                    <frame>
                        <vertical padding="10sp">
                            <vertical padding="8sp" marginTop="10sp" bg="#99FE01">
                                <horizontal>
                                    <checkbox id="zidongxiucheng" text="自动修城" />
                                    <checkbox id="mianfeizhaomu" text="免费招募" />
                                </horizontal>
                            </vertical>
                            <vertical padding="8sp" marginTop="10sp" bg="#2E7CEE">
                                <horizontal bg="#2E7CEE">
                                    <checkbox id="lianmengjuanzeng" textColor={colorHei} text="联盟捐赠" />
                                    <checkbox id="lianmenghuzhu" textColor={colorHei} text="联盟互助" />
                                </horizontal>
                            </vertical>
                            {/* 训练小兵 */}
                            <vertical marginTop="8sp" padding="8sp" bg="#99FE01">
                                <horizontal >
                                    <text text=" 是否开启：" textColor={colorHei} textSize="15sp" />
                                    <checkbox id="xunliansb" text="训练士兵" />
                                </horizontal>
                                <horizontal >
                                    <checkbox id="tfsxXunlian" text="讨伐上限一分钟以上训练" />
                                </horizontal>
                                <horizontal marginTop="2sp">
                                    <checkbox id="gongbing" text="弓兵" />
                                    <checkbox id="qibing" text="骑兵" />
                                    <checkbox id="bubing" text="步兵" />
                                    <checkbox id="chihou" text="斥候" />
                                </horizontal>
                                <horizontal>
                                    <text text="训练士兵间隔" textColor={colorHei} textSize="15sp" />
                                    <input text="30" id="shibingjg" textSize="16sp" inputType="number" padding="8 6" />
                                    <text text="分钟" textColor={colorHei} textSize="15sp" />
                                </horizontal>
                            </vertical>
                            <vertical padding="8sp" marginTop="8sp" bg="#FFFF01">
                                <horizontal >
                                    <text text=" 是否开启：" textColor={colorHei} textSize="15sp" />
                                    <checkbox id="openDixi" text="敌袭策略" />
                                </horizontal>
                                <horizontal  >
                                    <checkbox id="dixigaopinJc" text="敌袭高频检测(低性能手机,请勿使用)" />
                                </horizontal>
                                <horizontal >
                                    <checkbox id="dixiyuyin" text="敌袭语音提醒" />
                                    <checkbox id="dixizhendong" text="敌袭震动提醒" />
                                </horizontal>
                                <horizontal >
                                    <checkbox id="fanjiduiwu" text="敌袭反击队伍" />
                                    <checkbox id="dixichecaiji" text="敌袭撤回采集" />
                                </horizontal>
                                <vertical>
                                    <checkbox id="dixiyoujian" text="敌袭邮件提醒" />
                                    <input id="userrEmail" hint="请输入邮箱地址" text="" textSize="15sp" inputType="textEmailAddress" gravity="left" w="*" padding="8 6" />
                                </vertical>
                            </vertical>
                            {/* 升级建筑 */}
                            <vertical marginTop="8sp" padding="8sp" bg="#99FE01">
                                <horizontal >
                                    <text text=" 是否开启：" textColor={colorHei} textSize="15sp" />
                                    <checkbox id="sjShengjia" text="建筑升级" />
                                </horizontal>
                                <horizontal marginTop="2sp">
                                    <checkbox id="sjZhucdheng" text="主城" />
                                    <checkbox id="sjYingxiong" text="英雄" />
                                    <checkbox id="sjBingying" text="兵营" />
                                    <checkbox id="sjYanjiu" text="研究" />
                                </horizontal>
                                <horizontal>
                                    <checkbox id="sjChengfang" text="城防" />
                                    <checkbox id="sjSchang" text="市场" />
                                    <checkbox id="sjJunbei" text="军备" />
                                </horizontal>
                            </vertical>
                            <vertical padding="8sp" marginTop="10sp" bg="#E43C2F">
                                <horizontal  >
                                    <checkbox id="fanjizhucheng" text="敌袭反击主城" />
                                </horizontal>
                                <horizontal  >
                                    <checkbox id="zidongxiucheng" text="敌袭自动修城" />
                                    <checkbox id="yuanbaoxiuchehng" text="敌袭元宝修城" />
                                </horizontal>
                            </vertical>
                        </vertical>
                    </frame>
                </ScrollView>
                {/* 一键养号 */}
                <ScrollView>
                    <frame>
                        <vertical padding="10sp">
                            <vertical padding="8sp" marginTop="10sp" bg="#99FE01">
                                <vertical >
                                    <text text="侠投林刷扬州，先将游戏页面调整至需要刷的关卡头像处，然后点击运行" gravity="left" textColor={colorHei} textSize="14sp" />
                                    <text text="如需使用其他侠胆英雄请先手动刷一次，进入即使用默认英雄" gravity="left" textColor={colorHei} marginTop="6sp" textSize="10sp" />
                                </vertical>
                                <vertical marginTop="2sp">
                                    <button style="Widget.AppCompat.Button.Colored" textSize='14sp' id="xtlSuangyangzhou" marginTop="8sp">循环运行</button>
                                </vertical>
                            </vertical>
                            <vertical padding="8sp" marginTop="10sp" bg="#99FE01">
                                <vertical >
                                    <text text="通关探索用于清空赛季探索令,减少几百下的重复点击" gravity="left" textColor={colorHei} textSize="14sp" />
                                    <text text="将页面调整至探索令位置后点击按钮" gravity="left" textColor={colorHei} marginTop="6sp" textSize="10sp" />
                                </vertical>
                                <vertical marginTop="2sp">
                                    <button style="Widget.AppCompat.Button.Colored" textSize='14sp' id="tongguantansuo" marginTop="8sp">通关探索</button>
                                </vertical>
                            </vertical>
                            {/* <vertical padding="8sp" marginTop="10sp" bg="#E43C2F">
                                <vertical >
                                    <text text="运行后不采集不打野,只监测敌袭" gravity="left" textColor={colorHei} textSize="14sp" />
                                    <horizontal >
                                        <checkbox id="DDixizidongzhuhou" text="敌袭自动驻守" />
                                        <checkbox id="DDixiyuanbaoxiucheng" text="敌袭元宝修城" />
                                    </horizontal>
                                    <horizontal >
                                        <checkbox id="DDixiyuyin" text="敌袭语音提醒" />
                                        <checkbox id="DDixizhendong" text="敌袭震动提醒" />
                                    </horizontal>
                                    <vertical>
                                        <checkbox id="DDixiyoujian" text="敌袭邮件提醒" />
                                        <input id="userrEmail" hint="请输入邮箱地址" text="" textSize="15sp" inputType="textEmailAddress" gravity="left" w="*" padding="8 6" />
                                    </vertical>
                                </vertical>
                                <vertical marginTop="2sp">
                                    <button style="Widget.AppCompat.Button.Colored" textSize='14sp' id="beizhandingshao" marginTop="8sp">备战盯梢</button>
                                </vertical>
                            </vertical> */}
                            {/* 升级建筑 */}
                            <vertical marginTop="8sp" padding="8sp" bg="#E43C2F">
                                <horizontal marginTop="2sp">
                                    <checkbox id="lingzhouka" text="领周卡" />
                                    <checkbox id="lingueka" text="领月卡" />
                                    <checkbox id="kaihongbao" text="开红包" />
                                    <checkbox id="lingyoujian" text="领邮件" />
                                </horizontal>
                                <horizontal>
                                    <checkbox id="qinadaofuli" text="每月签到" />
                                    <checkbox id="fulivip" text="福利VIP" />
                                    <checkbox id="tanbao" text="探宝" />
                                </horizontal>
                                <horizontal>
                                    <checkbox id="zidongxiucheng" text="送军令" />
                                    <checkbox id="yinliangzhaomu" text="银两招募" />
                                    <checkbox id="yaoqianshu" text="摇钱树摇一次" />
                                </horizontal>
                                <vertical>
                                    <horizontal>
                                        <checkbox id="shuayangzhoua" text="闯关扬州韩信" />
                                    </horizontal>
                                    <horizontal>
                                        <text text=" 侠胆主将：" textColor={colorHei} textSize="15sp" />
                                        <spinner id="xiadanzhujiang" entries="林冲|朱元璋|任桓之|赵云" spinnerModer="dialog" />
                                    </horizontal>
                                    <horizontal>
                                        <checkbox id="saodang" text="闯关扫荡" />
                                    </horizontal>
                                    <horizontal>
                                        <text text=" 扫荡地图：" textColor={colorHei} textSize="15sp" />
                                        <spinner id="xiadanzhujiang" entries="天竺|大月氏|大宛|乌孙|疏勒" spinnerModer="dialog" />
                                    </horizontal>
                                </vertical>
                                <horizontal>
                                    <checkbox id="jingjichang" text="竞技场直接退" />
                                    <checkbox id="jingdianzjt" text="经典直接退" />
                                </horizontal>
                                <horizontal>
                                    <checkbox id="zhengtaosifang" text="领取征讨四方" />
                                </horizontal>
                                <vertical marginTop="2sp">
                                    <button style="Widget.AppCompat.Button.Colored" textSize='14sp' id="" marginTop="8sp">运行一次</button>
                                </vertical>
                            </vertical>
                            {/* 刷排位 */}
                            <vertical marginTop="8sp" padding="8sp" bg="#E43C2F">
                                <horizontal >
                                    <text text="排位直接退信誉值不足后进经典直接退循环刷满" gravity="left" textColor={colorHei} textSize="14sp" />
                                </horizontal>
                                <vertical marginTop="2sp">
                                    <button style="Widget.AppCompat.Button.Colored" textSize='14sp' id="" marginTop="8sp">运 行</button>
                                </vertical>
                            </vertical>
                        </vertical>
                    </frame>
                </ScrollView>
                {/* 系统设置 */}
                <ScrollView>
                    <frame>
                        <vertical padding="10sp">
                            <horizontal>
                                <text text="  激活码：" gravity="left" textColor={colorHei} textSize="15sp" />
                                <input id="jihuoma" hint="请输入注册码" text="" textSize="15sp" inputType="text" gravity="left" w="*" />
                            </horizontal>
                            <Switch id="autoService" text="无障碍服务" checked="{{auto.service != null}}" padding="8 8 8 8" textSize="15sp" />
                            <Switch id="floatyService" text="悬浮窗权限" checked="{{floaty.checkPermission()}}" padding="8 8 8 8" textSize="15sp" />
                            <vertical padding="10sp">
                                <text id="shebeiInfo" text=" 设备信息 " gravity="center" textSize="14sp" />
                            </vertical>
                            <vertical marginTop="10sp" padding="12sp" bg="#FFC801">
                                <horizontal>
                                    <checkbox id="logFile" text="日志文件" />
                                    <checkbox id="zhenjibaohuo" text="真机保活" />
                                    <checkbox id="xipingRoot" text="息屏运行(Root)" />
                                </horizontal>
                                <horizontal>
                                    <text text=" 识别算法：" textColor={colorHei} textSize="15sp" />
                                    <spinner id="shibiefangshi" entries="谷歌+百度算法|百度算法|谷歌算法" spinnerModer="dialog" />
                                </horizontal>
                                <horizontal>
                                    <text text="  延迟速率：" textColor={colorHei} textSize="15sp" />
                                    <input id="yanchisulv" hint="延迟速率" text="" textSize="15sp" inputType="text" gravity="center" />
                                    <text text=" (谨慎修改！)" textColor={colorHei} textSize="15sp" />
                                </horizontal>
                                <text text=" 真机保活保持真机不熄屏，请设置息屏时间在十分钟以上 " gravity="center" textSize="12sp" />
                            </vertical>
                            <vertical>
                                <button style="Widget.AppCompat.Button.Colored" textSize='14sp' id="closeQiang" marginTop="8sp">强制关闭</button>

                                <button style="Widget.AppCompat.Button.Colored" textSize='14sp' id="wangpan" marginTop="8sp">网盘更新 密码:8888</button>

                                <button style="Widget.AppCompat.Button.Colored" textSize='14sp' id="cleanData" marginTop="8sp">清空所有数据</button>

                                <button style="Widget.AppCompat.Button.Colored" textSize='14sp' id="testLingsheng" marginTop="8sp">算法支持检测(VIVO必测)</button>

                                <button style="Widget.AppCompat.Button.Colored" textSize='14sp' id="wentifankui" marginTop="8sp">问题反馈</button>

                                <button style="Widget.AppCompat.Button.Colored" textSize='14sp' id="kanrizhiwenjian" marginTop="8sp">日志文件</button>
                            </vertical>
                            <vertical marginTop="8sp">
                                {/* <text text="欢迎使用道一科技系列产品" gravity="center" textSize="15sp" /> */}
                                <text autoLink="web" text="http://autotouch.top/yxs.html" gravity="center" />
                                {/* <text text="抖/V: Daoyikeji88" gravity="center" /> */}
                                <text id="appVersion" text=" 当前软件版本： " gravity="center" textSize="12sp" marginTop="6sp" />
                            </vertical>
                        </vertical>
                    </frame>
                </ScrollView>
            </viewpager>
        </vertical>

        <vertical layout_gravity="left" bg="#ffffff" w="280">
            <img w="280" h="200" scaleType="fitXY" src="http://images.shejidaren.com/wp-content/uploads/2014/10/023746fki.jpg" />
            <list id="menu">
                <horizontal bg="?selectableItemBackground" w="*">
                    <img w="50" h="50" padding="16" src="{{this.icon}}" tint="{{colorHei}}" />
                    <text textColor="black" textSize="15sp" text="{{this.title}}" layout_gravity="center" />
                </horizontal>
            </list>
        </vertical>
    </drawer>
);

// 用户界面初始化配置
// 坐边三道杠 右上角三个点
activity.setSupportActionBar(ui.toolbar);
//设置滑动页面的标题
ui.viewpager.setTitles(["功能设置", "采集设置", "升级备战", "养号", "系统设置"]);
//让滑动页面和标签栏联动
ui.tabs.setupWithViewPager(ui.viewpager);
//让工具栏左上角可以打开侧拉菜单
ui.toolbar.setupWithDrawer(ui.drawer);
ui.menu.setDataSource([
    {
        title: "脚本设置",
        icon: "@drawable/ic_android_black_48dp"
    },
    {
        title: "运行日志",
        icon: "@drawable/ic_description_black_48dp"
    },
    {
        title: "权限管理",
        icon: "@drawable/ic_settings_black_48dp"
    },
    {
        title: "检测更新",
        icon: "@drawable/ic_refresh_black_48dp"
    },
    {
        title: "退出",
        icon: "@drawable/ic_exit_to_app_black_48dp"
    }
]);
ui.menu.on("item_click", item => {
    switch (item.title) {
        case "脚本设置":
            app.startActivity("settings");
            break;
        case "运行日志":
            app.startActivity("console");
            break;
        case "权限管理":
            app.openAppSetting("com.daoyikeji.autoYxs");
            break;
        case "检测更新":
            jianceGengxinFunc();
            break;
        case "退出":
            ui.finish();
            break;
    }
})
//创建选项菜单(右上角)
ui.emitter.on("create_options_menu", menu => {
    menu.add("保存");
    menu.add("运行");
});
//监听选项菜单点击
ui.emitter.on("options_item_selected", (e, item) => {
    e.consumed = true;
    switch (item.getTitle()) {
        case "保存":
            saveconfig();
            toastLog("配置已保存");
            break;
        case "运行":
            saveconfig();
            //程序开始运行之前判断无障碍服务
            if (auto.service == null) {
                toastLog("请先开启无障碍服务！");
                app.startActivity({
                    action: "android.settings.ACCESSIBILITY_SETTINGS"
                });
                return;
            } else {
                log('已获取无障碍权限');
            }
            // 悬浮窗权限
            if (!floaty.checkPermission()) {
                // 没有悬浮窗权限，提示用户并跳转请求
                toastLog("请开启悬浮窗权限来显示悬浮窗，请在随后的界面中允许并重新运行本脚本。");
                floaty.requestPermission();
                return;
            } else {
                log('已获取悬浮窗权限');
            }
            // 激活码
            if (storage.get("jihuoma", "").length != 28) {
                toastLog("激活码错误，请打开系统设置选项验证激活码！");
                return;
            }
            // 设备信息   
            initDataFunc();
            // 右上悬浮窗
            xuanfuFunc()
            // 日志文件
            if (Boolean(storage.get("logFile", false))) {
                console.setGlobalLogConfig({
                    "file": "/sdcard/autoYxs.txt"
                });
            }
            log("开始")
            storage.put("firstRun", true);
            storage.put("correctEntry", true);
            myLogFunc('分辨率：' + deviceWidth + '*' + deviceHeight)
            myLogFunc('分辨率基准：' + picJizhun)
            myScriptEngine = engines.execScriptFile("./index.js", {
                loopTimes: 0,
                interval: 1000
            });

            threads.start(function () {
                setInterval(function () {
                    if (floatyWindow && myScriptEngine && Boolean(storage.get("jinchengshouhu", true)) && storage.get("yunxingfangshi", 1) == 1) {
                        if (myScriptEngine.getEngine().isDestroyed()) {
                            log("进程守护程序")
                            myScriptEngine.getEngine().forceStop()
                            myScriptEngine = null;
                            myScriptEngine = engines.execScriptFile("./index.js", {
                                loopTimes: 0,
                                interval: 5000
                            });
                        }
                    }
                }, 60000);
            });

            log("已开始")
            break;
    }
});

// 用户界面数据配置
initJiemianFunc()
// 读取配置写入界面
readconfig()
function initJiemianFunc() {
    let str = "";
    str += "" + device.brand + " " + device.board + " " + device.model;
    str += "\n分辨率:" + device.width + " x " + device.height;
    str += "\n安卓版本: " + device.release + " API: " + device.sdkInt;
    str += "\nAndroidId: " + device.getAndroidId();
    ui.shebeiInfo.text(" " + str + "     ");
    ui.appVersion.text(" 当前软件版本：" + project.versionName + "     ");
    ui.toolbar.title = "世界模式 挂机辅助 " + project.versionName;
    threads.start(function () {
        // 请求服务
        let reqs = http.postJson("http://autotouch.top:8866/main/v1/auto/xysjNotice", {
            activationCode: storage.get("jihuoma", ""),
            aap: "yxsXysj",
            deviceWidth: device.width,
            deviceHeight: device.height,
            brand: device.brand,
            versionCode: project.versionCode,
            release: device.release,
            sdkInt: device.sdkInt,
            androidId: device.getAndroidId(),
            fingerprint: device.fingerprint,
            startTime: new Date().getTime()
        });
        let rJson = reqs.body.json();
        if (rJson.code == 200) {
            ui.run(function () {
                ui.gonggaoa.text(rJson.data + "     ");
            });
        } else {
            toastLog(rJson.message)
            ui.finish()
        }
    })
}
// 检测更新
function jianceGengxinFunc() {
    threads.start(function () {
        // 请求服务
        let reqs = http.postJson("http://autotouch.top:8866/main/v1/auto/detectVersion", {
            activationCode: storage.get("jihuoma", ""),
            aap: "yxsXysj",
            deviceWidth: device.width,
            deviceHeight: device.height,
            brand: device.brand,
            versionCode: project.versionCode,
            release: device.release,
            sdkInt: device.sdkInt,
            androidId: device.getAndroidId(),
            fingerprint: device.fingerprint,
            startTime: new Date().getTime()
        });
        let rJson = reqs.body.json();
        if (rJson.code == 200) {
            toastLog(rJson.message)
        } else {
            toastLog(rJson.message)
            let releaseNotes = "新版本:" + rJson.data.versionName + "\n" + "更新日志:\n" + rJson.data.versionNotice + "\n"
            dialogs.build({
                title: "发现新版本",
                content: releaseNotes,
                positive: "网盘下载(密码:8888)",
                negative: "取消"
            })
                .on("positive", () => {
                    app.openUrl("https://stack.lanzouh.com/b013wfexe");
                })
                .show();
            // alert("新版本:" + rJson.data.versionName + "\n" + "更新日志:\n" + rJson.data.versionNotice + "\n" + "请前往系统设置 网盘下载");
        }
    })
}
// 用户输出
function myLogFunc() {
    log('myLogFunc', arguments)
}
// 保存配置
function saveconfig() {
    storage.put("jihuoma", ui.jihuoma.text());
    storage.put("tianxuan", ui.tianxuan.checked);
    storage.put("junbao", ui.junbao.checked);
    storage.put("zhengshou", ui.zhengshou.checked);
    storage.put("lingqurenwu", ui.lingqurenwu.checked)
    storage.put("isdaye", ui.isdaye.checked)
    storage.put("ye1", ui.ye1.checked)
    storage.put("ye2", ui.ye2.checked)
    storage.put("ye3", ui.ye3.checked)
    storage.put("ye4", ui.ye4.checked)
    storage.put("ye5", ui.ye5.checked)
    storage.put("ye6", ui.ye6.checked)
    storage.put("ye7", ui.ye7.checked)
    storage.put("shuayewushang", ui.shuayewushang.checked)
    storage.put("fangqiangye", ui.fangqiangye.checked);
    storage.put("chihouzhencha", ui.chihouzhencha.checked);
    storage.put("lianmengjuanzeng", ui.lianmengjuanzeng.checked);
    storage.put("lianmenghuzhu", ui.lianmenghuzhu.checked);
    storage.put("caijiquyuDy", ui.caijiquyuDy.checked);
    storage.put("zuoshang", ui.zuoshang.checked);
    storage.put("zuozhong", ui.zuozhong.checked);
    storage.put("zuoxia", ui.zuoxia.checked);
    storage.put("youshang", ui.youshang.checked);
    storage.put("youzhong", ui.youzhong.checked);
    storage.put("youxia", ui.youxia.checked);
    storage.put("jaincedenglu", ui.jaincedenglu.checked);
    storage.put("jinchengshouhu", ui.jinchengshouhu.checked);
    storage.put("shuayezhanli", ui.shuayezhanli.getSelectedItemPosition());
    storage.put("denglufangshi", ui.denglufangshi.getSelectedItemPosition());
    storage.put("shibiefangshi", ui.shibiefangshi.getSelectedItemPosition());
    storage.put("shibiefangshi", ui.shibiefangshi.getSelectedItemPosition());
    storage.put("yunxingfangshi", ui.yunxingfangshi.getSelectedItemPosition());
    storage.put("otherQqLogin", ui.otherQqLogin.checked);
    storage.put("otherQqAccount", ui.otherQqAccount.text());

    // 采集配置
    storage.put("caijiziyuan", ui.caijiziyuan.checked);
    storage.put("zhicaifujin", ui.zhicaifujin.checked);
    storage.put("bucaijishiduan", ui.bucaijishiduan.checked);
    storage.put("beixuanziyuan", ui.beixuanziyuan.checked);
    storage.put("caijiyouxian", ui.caijiyouxian.getSelectedItemPosition());
    storage.put("baohanziji", ui.baohanziji.checked);
    storage.put("centerZhucheng", ui.centerZhucheng.checked);
    storage.put("changfangxingDy", ui.changfangxingDy.checked);
    storage.put("lingtai", ui.lingtai.checked);
    storage.put("isSanguo", ui.isSanguo.checked);
    storage.put("islingdidenglu", ui.islingdidenglu.checked);
    storage.put("beixuanziyuanFw", ui.beixuanziyuanFw.text());
    storage.put("bucaijishiduan1", ui.bucaijishiduan1.text());
    storage.put("bucaijishiduan2", ui.bucaijishiduan2.text());
    storage.put("centerNPing", ui.centerNPing.text());
    storage.put("fangxingPing", ui.fangxingPing.text());
    storage.put("intermission", ui.intermission.text());
    storage.put("lingdidenglu", ui.lingdidenglu.text());
    storage.put("ziyuancunchu", ui.ziyuancunchu.checked);
    storage.put("quziyuan", ui.quziyuan.checked);
    storage.put("qiangzhidufen", ui.qiangzhidufen.checked);
    storage.put("dongtaizyyouxian", ui.dongtaizyyouxian.checked);
    storage.put("caijibaoliuHero", ui.caijibaoliuHero.text());
    storage.put("shuayebaoliu", ui.shuayebaoliu.text());
    storage.put("cunliangNum", ui.cunliangNum.text());
    storage.put("cunliangNumSy", ui.cunliangNumSy.text());

    // 升级备战
    storage.put("zidongxiucheng", ui.zidongxiucheng.checked);
    storage.put("mianfeizhaomu", ui.mianfeizhaomu.checked);
    storage.put("xunliansb", ui.xunliansb.checked);
    storage.put("tfsxXunlian", ui.tfsxXunlian.checked);
    storage.put("gongbing", ui.gongbing.checked);
    storage.put("qibing", ui.qibing.checked);
    storage.put("bubing", ui.bubing.checked);
    storage.put("chihou", ui.chihou.checked);
    storage.put("shibingjg", ui.shibingjg.text());
    storage.put("openDixi", ui.openDixi.checked);
    storage.put("dixigaopinJc", ui.dixigaopinJc.checked);
    storage.put("dixiyuyin", ui.dixiyuyin.checked);
    storage.put("dixizhendong", ui.dixizhendong.checked);
    storage.put("dixichecaiji", ui.dixichecaiji.checked);
    storage.put("fanjiduiwu", ui.fanjiduiwu.checked);
    storage.put("dixiyoujian", ui.dixiyoujian.checked);
    storage.put("userrEmail", ui.userrEmail.text());

    storage.put("sjShengjia", ui.sjShengjia.checked);
    storage.put("sjZhucdheng", ui.sjZhucdheng.checked);
    storage.put("sjYingxiong", ui.sjYingxiong.checked);
    storage.put("sjBingying", ui.sjBingying.checked);
    storage.put("sjYanjiu", ui.sjYanjiu.checked);
    storage.put("sjChengfang", ui.sjChengfang.checked);
    storage.put("sjSchang", ui.sjSchang.checked);
    storage.put("sjJunbei", ui.sjJunbei.checked);

    storage.put("logFile", ui.logFile.checked);
    storage.put("zhenjibaohuo", ui.zhenjibaohuo.checked);
    storage.put("xipingRoot", ui.xipingRoot.checked);
    storage.put("caijiItems", toCaijiList);
    if (Number(ui.yanchisulv.text()) < 0.5) {
        storage.put("delayBase", "0.5")
    } else {
        storage.put("delayBase", ui.yanchisulv.text())
    }
}
// 读取配置写入界面
function readconfig() {
    ui.jihuoma.text(storage.get("jihuoma", ""));
    ui.tianxuan.checked = Boolean(storage.get("tianxuan", true));
    ui.junbao.checked = Boolean(storage.get("junbao", true));
    ui.zhengshou.checked = Boolean(storage.get("zhengshou", false));
    ui.lingqurenwu.checked = Boolean(storage.get("lingqurenwu", false));
    ui.isdaye.checked = Boolean(storage.get("isdaye", true));
    ui.ye1.checked = Boolean(storage.get("ye1", false));
    ui.ye2.checked = Boolean(storage.get("ye2", false));
    ui.ye3.checked = Boolean(storage.get("ye3", false));
    ui.ye4.checked = Boolean(storage.get("ye4", false));
    ui.ye5.checked = Boolean(storage.get("ye5", false));
    ui.ye6.checked = Boolean(storage.get("ye6", false));
    ui.ye7.checked = Boolean(storage.get("ye7", false));
    ui.shuayewushang.checked = Boolean(storage.get("shuayewushang", false));
    ui.fangqiangye.checked = Boolean(storage.get("fangqiangye", false));
    ui.chihouzhencha.checked = Boolean(storage.get("chihouzhencha", false));
    ui.lianmengjuanzeng.checked = Boolean(storage.get("lianmengjuanzeng", false));
    ui.lianmenghuzhu.checked = Boolean(storage.get("lianmenghuzhu", false));
    ui.caijiquyuDy.checked = Boolean(storage.get("caijiquyuDy", false));
    ui.zuoshang.checked = Boolean(storage.get("zuoshang", false));
    ui.zuozhong.checked = Boolean(storage.get("zuozhong", false));
    ui.zuoxia.checked = Boolean(storage.get("zuoxia", false));
    ui.youshang.checked = Boolean(storage.get("youshang", false));
    ui.youzhong.checked = Boolean(storage.get("youzhong", false));
    ui.youxia.checked = Boolean(storage.get("youxia", false));
    ui.jaincedenglu.checked = Boolean(storage.get("jaincedenglu", false));
    ui.jinchengshouhu.checked = Boolean(storage.get("jinchengshouhu", true));
    ui.otherQqLogin.checked = Boolean(storage.get("otherQqLogin", false));
    if (parseInt(storage.get("shuayezhanli", 0))) {
        ui.shuayezhanli.setSelection(parseInt(storage.get("shuayezhanli", 0)));
    };
    if (parseInt(storage.get("denglufangshi", 0))) {
        ui.denglufangshi.setSelection(parseInt(storage.get("denglufangshi", 0)));
    };
    if (parseInt(storage.get("shibiefangshi", 0))) {
        ui.shibiefangshi.setSelection(parseInt(storage.get("shibiefangshi", 0)));
    };
    if (parseInt(storage.get("yunxingfangshi", 1))) {
        ui.yunxingfangshi.setSelection(parseInt(storage.get("yunxingfangshi", 1)));
    };
    if (parseInt(storage.get("caijiyouxian", 0))) {
        ui.caijiyouxian.setSelection(parseInt(storage.get("caijiyouxian", 0)));
    };
    ui.otherQqAccount.text(storage.get("otherQqAccount", ""));
    // 采集配置
    ui.caijiziyuan.checked = Boolean(storage.get("caijiziyuan", true));
    ui.zhicaifujin.checked = Boolean(storage.get("zhicaifujin", false));
    ui.bucaijishiduan.checked = Boolean(storage.get("bucaijishiduan", false));
    ui.beixuanziyuan.checked = Boolean(storage.get("beixuanziyuan", true));
    ui.beixuanziyuanFw.text(storage.get("beixuanziyuanFw", "3"));
    ui.bucaijishiduan1.text(storage.get("bucaijishiduan1", "1"));
    ui.bucaijishiduan2.text(storage.get("bucaijishiduan2", "5"));
    ui.centerNPing.text(storage.get("centerNPing", "1"));
    ui.fangxingPing.text(storage.get("fangxingPing", "108,88,138,108"));
    ui.intermission.text(storage.get("intermission", "1"));
    ui.lingdidenglu.text(storage.get("lingdidenglu", "20"));
    ui.baohanziji.checked = Boolean(storage.get("baohanziji", false));
    ui.centerZhucheng.checked = Boolean(storage.get("centerZhucheng", true));
    ui.changfangxingDy.checked = Boolean(storage.get("changfangxingDy", false));
    ui.lingtai.checked = Boolean(storage.get("lingtai", false));
    ui.isSanguo.checked = Boolean(storage.get("isSanguo", false));
    ui.islingdidenglu.checked = Boolean(storage.get("islingdidenglu", false));
    ui.dongtaizyyouxian.checked = Boolean(storage.get("dongtaizyyouxian", true));
    ui.ziyuancunchu.checked = Boolean(storage.get("ziyuancunchu", false));
    ui.quziyuan.checked = Boolean(storage.get("quziyuan", false));
    ui.qiangzhidufen.checked = Boolean(storage.get("qiangzhidufen", false));
    ui.caijibaoliuHero.text(storage.get("caijibaoliuHero", "0"));
    ui.shuayebaoliu.text(storage.get("shuayebaoliu", "0"));
    ui.cunliangNum.text(storage.get("cunliangNum", "19999"));
    ui.cunliangNumSy.text(storage.get("cunliangNumSy", "150"));

    // 升级备战
    ui.zidongxiucheng.checked = Boolean(storage.get("zidongxiucheng", true));
    ui.mianfeizhaomu.checked = Boolean(storage.get("mianfeizhaomu", false));
    ui.xunliansb.checked = Boolean(storage.get("xunliansb", false));
    ui.tfsxXunlian.checked = Boolean(storage.get("tfsxXunlian", false));
    ui.gongbing.checked = Boolean(storage.get("gongbing", false));
    ui.qibing.checked = Boolean(storage.get("qibing", false));
    ui.bubing.checked = Boolean(storage.get("bubing", false));
    ui.chihou.checked = Boolean(storage.get("chihou", false));
    ui.shibingjg.text(storage.get("shibingjg", "30"));
    ui.openDixi.checked = Boolean(storage.get("openDixi", false));
    ui.dixigaopinJc.checked = Boolean(storage.get("dixigaopinJc", false));
    ui.dixiyuyin.checked = Boolean(storage.get("dixiyuyin", false));
    ui.dixizhendong.checked = Boolean(storage.get("dixizhendong", false));
    ui.dixichecaiji.checked = Boolean(storage.get("dixichecaiji", false));
    ui.fanjiduiwu.checked = Boolean(storage.get("fanjiduiwu", false));
    ui.dixiyoujian.checked = Boolean(storage.get("dixiyoujian", false));
    ui.userrEmail.text(storage.get("userrEmail", ""));

    ui.sjShengjia.checked = Boolean(storage.get("sjShengjia", false));
    ui.sjZhucdheng.checked = Boolean(storage.get("sjZhucdheng", false));
    ui.sjYingxiong.checked = Boolean(storage.get("sjYingxiong", false));
    ui.sjBingying.checked = Boolean(storage.get("sjBingying", false));
    ui.sjYanjiu.checked = Boolean(storage.get("sjYanjiu", false));
    ui.sjChengfang.checked = Boolean(storage.get("sjChengfang", false));
    ui.sjSchang.checked = Boolean(storage.get("sjSchang", false));
    ui.sjJunbei.checked = Boolean(storage.get("sjJunbei", false));

    ui.logFile.checked = Boolean(storage.get("logFile", false));
    ui.zhenjibaohuo.checked = Boolean(storage.get("zhenjibaohuo", false));
    ui.xipingRoot.checked = Boolean(storage.get("xipingRoot", false));
    ui.yanchisulv.text(String(storage.get("delayBase", "1")));
}

// 设置数据源
ui.ziyuanList.setDataSource(toCaijiList);
// 数据转化
function ziyuanGuolvFunc(params) {
    let a = '粮'
    if (params == '0') {
        a = '粮'
    }
    if (params == '1') {
        a = '木'
    }
    if (params == '2') {
        a = '矿'
    }
    return a;
}
// 添加
ui.ziyuanAdd.on("click", function () {
    dialogs.rawInput("资源信息(X,Y,等级)", "88,66,5")
        .then(title => {
            if (!title) {
                return;
            }
            let arr = title.split(',')
            if (!title || arr.length != 3) {
                log("资源格式不正确")
                return;
            }
            dialogs.select("请选择资源类型", ["粮田", "木头", "铁矿"])
                .then(i => {
                    toCaijiList.push({
                        x: arr[0],
                        y: arr[1],
                        level: arr[2],
                        type: i
                    });
                    log("添加资源", toCaijiList)
                });
        })
})
// 清空
ui.ziyuanClean.on("click", function () {
    toCaijiList.splice(0, toCaijiList.length);
    toastLog('清空资源配置')
})
// 导出
ui.zyDaochu.on("click", function () {
    log("资源", toCaijiList)
    let copyList = ''
    for (let index = 0; index < toCaijiList.length; index++) {
        let element = toCaijiList[index];
        copyList += element.x + "," + element.y + "," + element.level + "," + element.type + '\n'
    }
    log("copyList", copyList)
    setClip(copyList);
    toastLog('已复制')
})
// 导入
ui.zyDaoru.on("click", function () {
    let getClipSs = getClip()
    getClipSs = getClipSs.split('\n')
    log(getClipSs);
    for (let index = 0; index < getClipSs.length; index++) {
        let element = getClipSs[index];
        element = element.split(',')
        if (element.length > 2) {
            let level = element[2]
            let type = element[3]
            if (!level) {
                level = 1
            } else {
                level = level.replace(/\D/g, "");
                log('处理level', level);
            }
            if (!type) {
                type = 1
            } else {
                type = type.replace(/\D/g, "");
                log('处理type', type);
            }
            toCaijiList.push({ x: element[0], y: element[1], level: level, type: type })
        }
    }
    log(toCaijiList);
    storage.put("caijiItems", toCaijiList);
    toastLog('已导入并保存')
})

// 按指定规则排序
function sortByTypeAndLevel (array, typeValue) {
    return array.sort((a, b) => {
      if (a.type === typeValue && b.type === typeValue) {
        return b.level - a.level; // 当 type 等于 typeValue 时，按 level 降序排序
      }
      if (a.type === typeValue) {
        return -1; // a 的 type 是 typeValue，优先排在前面
      }
      if (b.type === typeValue) {
        return 1; // b 的 type 是 typeValue，优先排在前面
      }
      return b.level - a.level; // 否则，按 level 降序排序
    });
  };
// 等级排序
ui.dengjiPaixu.on("click", function () {
    log("资源列表", toCaijiList)
    // 按照 level 从大到小排序
    toCaijiList.sort((a, b) => b.level - a.level);
    // toCaijiList = sortByTypeAndLevel(toCaijiList, 0)
    storage.put("caijiItems", toCaijiList);
    ui.ziyuanList.setDataSource(toCaijiList);
    toastLog('已根据等级排序')
    
})

// 粮食排序
ui.dengjiPaixu0.on("click", function () {
    log("资源列表", toCaijiList)
    toCaijiList = sortByTypeAndLevel(toCaijiList, 0)
    storage.put("caijiItems", toCaijiList);
    ui.ziyuanList.setDataSource(toCaijiList);
    toastLog('已根据粮食排序')
    
})
// 木头排序
ui.dengjiPaixu1.on("click", function () {
    log("资源列表", toCaijiList)
    toCaijiList = sortByTypeAndLevel(toCaijiList, 1)
    storage.put("caijiItems", toCaijiList);
    ui.ziyuanList.setDataSource(toCaijiList);
    toastLog('已根据木头排序')
    
})
// 狂排序
ui.dengjiPaixu2.on("click", function () {
    log("资源列表", toCaijiList)
    toCaijiList = sortByTypeAndLevel(toCaijiList, 2)
    storage.put("caijiItems", toCaijiList);
    ui.ziyuanList.setDataSource(toCaijiList);
    toastLog('已根据铁矿排序')
    
})
// 保存
ui.ziyuanSave.on("click", function () {
    log("保存资源", toCaijiList)
    storage.put("caijiItems", toCaijiList);
    toastLog('配置已保存')
})

// 删除
ui.ziyuanList.on('item_bind', (itemView, itemHolder) => {
    itemView.delete.on('click', () => {
        log(itemHolder.position, toCaijiList)
        toCaijiList.splice(itemHolder.position, 1);
    });
});
// 养号操作
// 侠投林刷扬州
ui.xtlSuangyangzhou.on("click", function () {
    saveconfig();
    //程序开始运行之前判断无障碍服务
    if (auto.service == null) {
        toastLog("请先开启无障碍服务！");
        app.startActivity({
            action: "android.settings.ACCESSIBILITY_SETTINGS"
        });
        return;
    } else {
        log('已获取无障碍权限');
    }
    // 悬浮窗权限
    if (!floaty.checkPermission()) {
        // 没有悬浮窗权限，提示用户并跳转请求
        toastLog("请开启悬浮窗权限来显示悬浮窗，请在随后的界面中允许并重新运行本脚本。");
        floaty.requestPermission();
        return;
    } else {
        log('已获取悬浮窗权限');
    }
    // 激活码
    if (storage.get("jihuoma", "").length != 28) {
        toastLog("激活码错误，请打开系统设置选项验证激活码！");
        return;
    }
    // 设备信息   
    initDataFunc();
    // 右上悬浮窗
    xuanfuFunc()

    log("开始")
    myLogFunc('分辨率：' + deviceWidth + '*' + deviceHeight)
    myLogFunc('分辨率基准：' + picJizhun)
    storage.put("xunhuanrenwu", 'shuayangzhou');
    dialogs.select("选择关卡", ["步兵二(玄精云母铁)", "弓兵二(云母黄琥珀)", "韩信", "刘邦", "项羽", "吕雉"]).then(i => {
        log('第几关', i)
        storage.put("yangzhouShuia", i);
        myScriptEngine = engines.execScriptFile("./shuayangzhou.js", {
            loopTimes: 0,
            interval: 3000
        });
        log("已开始")
    });
})

// 通关探索 
ui.tongguantansuo.on("click", function () {
    saveconfig();
    //程序开始运行之前判断无障碍服务
    if (auto.service == null) {
        toastLog("请先开启无障碍服务！");
        app.startActivity({
            action: "android.settings.ACCESSIBILITY_SETTINGS"
        });
        return;
    } else {
        log('已获取无障碍权限');
    }
    // 悬浮窗权限
    if (!floaty.checkPermission()) {
        // 没有悬浮窗权限，提示用户并跳转请求
        toastLog("请开启悬浮窗权限来显示悬浮窗，请在随后的界面中允许并重新运行本脚本。");
        floaty.requestPermission();
        return;
    } else {
        log('已获取悬浮窗权限');
    }
    // 激活码
    if (storage.get("jihuoma", "").length != 28) {
        toastLog("激活码错误，请打开系统设置选项验证激活码！");
        return;
    }
    // 设备信息   
    initDataFunc();
    // 右上悬浮窗
    xuanfuFunc()
    // 注册服务
    myLogFunc("服务成功注册！")
    // 开始运行
    myLogFunc('分辨率：' + deviceWidth + '*' + deviceHeight)
    myLogFunc('分辨率基准：' + picJizhun)
    storage.put("xunhuanrenwu", 'tansuoling');
    myScriptEngine = engines.execScriptFile("./shuayangzhou.js", {
        loopTimes: 0,
        interval: 3000
    });
    log("已开始")
})

// 网盘更新
ui.wangpan.on("click", function () {
    app.openUrl('https://stack.lanzouh.com/b013wfexe')
})
// 清除数据
ui.cleanData.on("click", function () {
    storages.remove('autotouchShijieXy')
    toastLog('数据已清空，请重启APP')
    ui.finish()
})
// 清除数据
ui.testLingsheng.on("click", function () {
    threads.start(function () {
        //程序开始运行之前判断无障碍服务
        if (auto.service == null) {
            toastLog("请先开启无障碍服务！");
            app.startActivity({
                action: "android.settings.ACCESSIBILITY_SETTINGS"
            });
            return;
        } else {
            log('已获取无障碍权限');
        }
        // 悬浮窗权限
        if (!floaty.checkPermission()) {
            // 没有悬浮窗权限，提示用户并跳转请求
            toastLog("请开启悬浮窗权限来显示悬浮窗，请在随后的界面中允许并重新运行本脚本。");
            floaty.requestPermission();
            return;
        } else {
            log('已获取悬浮窗权限');
        }
        //申请截屏权限
        if (!requestScreenCapture(true)) {
            toastLog("请求截图失败");
            exit()
        }
        toastLog("谷歌算法测试")
        let newtime = new Date().getTime();
        let orcRes = gmlkit.ocr(captureScreen(), "zh");
        let newtime1 = new Date().getTime();
        toastLog("谷歌算法用时" + (newtime1 - newtime) + "毫秒")
        // orcRes = orcRes.text;
        // log(orcRes)
        sleep(1000)
        toastLog("百度算法测试")
        newtime = new Date().getTime();
        let orcRes1 = paddle.ocrText(captureScreen());
        newtime1 = new Date().getTime();
        toastLog("百度算法用时" + (newtime1 - newtime) + "毫秒")
        // orcRes1 = orcRes1[0]
        // log(orcRes)
    });
})
// 强行挂壁
ui.closeQiang.on("click", function () {
    storage.put("zanting", false);
    floaty.closeAll()
    threads.shutDownAll()
    if (myScriptEngine) {
        myScriptEngine.getEngine().emit("say", "close");
        myScriptEngine.getEngine().forceStop()
    }
    toastLog('强行结束运行')
    ui.finish()
    engines.stopAll()
})
// 看日志文件
ui.kanrizhiwenjian.on("click", function () {
    log(1)
    app.viewFile("/sdcard/autoYxs.txt");
})
// 问题反馈
ui.wentifankui.on("click", function () {
    dialogs.rawInput("您要反馈的问题或建议", "", function (str) {
        if (str) {
            dialogs.rawInput("您的微信号是？", "", function (strs) {
                if (strs) {
                    // 请求服务
                    threads.start(function () {
                        let reqs = http.postJson("http://autotouch.top:8866/main/v1/auto/problemBack", {
                            activationCode: storage.get("jihuoma", ""),
                            aap: "yxsXysj",
                            deviceWidth: device.width,
                            deviceHeight: device.height,
                            brand: device.brand,
                            versionCode: project.versionCode,
                            release: device.release,
                            sdkInt: device.sdkInt,
                            androidId: device.getAndroidId(),
                            fingerprint: device.fingerprint,
                            problemDescribe: str,
                            contactUs: strs
                        });
                        let rJson = reqs.body.json();
                        if (rJson.code == 200) {
                            toastLog("反馈已提交，感谢您的建议")
                        } else {
                            toastLog("系统繁忙！")
                        }
                    })
                }
            })
        }
    });
})
// 无障碍权限
ui.autoService.on("check", function (checked) {
    if (checked && auto.service == null) {
        app.startActivity({
            action: "android.settings.ACCESSIBILITY_SETTINGS"
        });
    }
    if (!checked && auto.service != null) {
        auto.service.disableSelf();
    }
});
// 悬浮窗权限
ui.floatyService.on("check", function (checked) {
    if (!floaty.checkPermission()) {
        // 没有悬浮窗权限，提示用户并跳转请求
        toastLog("请开启悬浮窗权限来显示悬浮窗，请在随后的界面中允许并重新运行本脚本。");
        floaty.requestPermission();
        return;
    } else {
        log('已获取悬浮窗权限');
    }
});

// 运行时方法
// 设备基准
function initDataFunc() {
    // 基准
    deviceWidth = device.width > device.height ? device.width : device.height;
    deviceHeight = device.height > device.width ? device.width : device.height;
    picJizhun = deviceHeight / 1080;
    if (deviceHeight == 720) {
        picJizhun = 0.666666666666667;
    }
    if (deviceHeight == 1200) {
        picJizhun = 1.12;
    }
    picJizhunHeight = picJizhun;
    log('基准数据：', deviceWidth, deviceHeight, picJizhun)
    storage.put("zanting", false);
    log('初始化数据完成')
}

// 悬浮窗
function xuanfuFunc(bloo) {
    if (floatyWindow) {
        floatyWindow.close();
    }
    floatyWindow = floaty.window(
        <horizontal >
            <button id="floatyAction" text="运" textSize="9sp" w="25" h="25" padding="0sp"></button>
            <button id="peizhishezhi" text="设置" textSize="8sp" w="38" h="25" bg="#7E8EBF" marginLeft="8sp" padding="0sp" />
            <button id="ziyuanpeizhi" text="资源" textSize="8sp" w="38" h="25" bg="#7E8EBF" marginLeft="2sp" padding="0sp" />
            <button id="myExit" text="退" textSize="8sp" w="25" h="25" bg="#02FE99" marginLeft="2sp" padding="0sp" />
        </horizontal >
    );

    floatyWindow.floatyAction.setOnTouchListener(function (view, event) {
        switch (event.getAction()) {
            case event.ACTION_DOWN:
                x = event.getRawX();
                y = event.getRawY();
                windowX = floatyWindow.getX();
                windowY = floatyWindow.getY();
                return true;
            case event.ACTION_MOVE:
                //移动手指时调整悬浮窗位置
                floatyWindow.setPosition(windowX + (event.getRawX() - x), windowY + (event.getRawY() - y));
                return true;
            case event.ACTION_UP:
                //手指弹起时如果偏移很小则判断为点击
                if (Math.abs(event.getRawY() - y) < 5 && Math.abs(event.getRawX() - x) < 5) {
                    // myLogFunc('点击悬浮窗')
                    floatyActionOnClick();
                }
                return true;
        }
        return true;
    });
    floatyWindow.myExit.on("click", function () {
        storage.put("zanting", false);
        storage.put("correctEntry", false);
        floaty.closeAll()
        threads.shutDownAll()
        myScriptEngine.getEngine().emit("say", "close");
        myScriptEngine.getEngine().forceStop()
        engines.stopAll()
    })
    floatyWindow.peizhishezhi.on("click", function () {
        app.launchPackage("com.daoyikeji.autoYxs");
    })

    function findMatch(str) {
        // 查找 '速度加成' 及其后面的第一个字符
        str = str.toString();
        const match = str.match(/速度加.(.)/);
        
        // 如果找到匹配项
        if (match) {
            const nextChar = match[1]; // '速度加成'后面的第一个字符
            
            // 判断是否是 1-7 的数字
            if ("1234567".includes(nextChar)) {
                return nextChar; // 返回匹配的数字
            }
        }
        
        // 如果不是 1-7，或者没有找到 '速度加成'，返回 1
        return "1";
    }
    // 资源配置
    floatyWindow.ziyuanpeizhi.on("click", function () {
        // 首次配置资源坐标位置
        threads.start(function () {
            log("资源坐标位置", ziyuanZuobianWeizhi.x)
            //申请截屏权限
            if (!jietuquanxian) {
                if (!requestScreenCapture(true)) {
                    toastLog("请求截图失败");
                    ui.finish()
                } else {
                    jietuquanxian = true
                    toastLog("已获取截图权限");
                }
            }
            if (ziyuanZuobianWeizhi.x == 0) {
                log(2)
                toastLog('首次配置资源坐标位置')
                log(3)
                let thisImgs1 = images.read("./res/坐标背景图.png");
                log(4)
                thisImgs1 = images.scale(thisImgs1, picJizhun, picJizhunHeight)
                log(5)
                let p1 = findImage(captureScreen(), thisImgs1, { region: [0, 0, (deviceWidth / 2), deviceHeight], threshold: 0.78 });
                log(6)
                thisImgs1.recycle();
                log("p1", p1)
                if (p1) {
                    log('资源坐标位置配置完成')
                    ziyuanZuobianWeizhi.x = p1.x + 50
                    ziyuanZuobianWeizhi.y = p1.y
                    storage.put("ziyuanZuobianWeizhi", ziyuanZuobianWeizhi);
                } else {
                    alert('资源坐标失败,先打开资源！')
                    toastLog('资源坐标失败,请打开资源！')
                    return
                }
            }
            if (ziyuanZuobianWeizhi.x == 0) {
                alert('资源坐标失败,先打开资源！')
                toastLog('资源坐标失败,请打开资源！')
                return
            }
            log(8)
            let clip = images.clip(captureScreen(), ziyuanZuobianWeizhi.x - 80, ziyuanZuobianWeizhi.y, (400 * picJizhun), (220 * picJizhunHeight));
            let orcRes = "";
            if (parseInt(storage.get("shibiefangshi", 0)) == 0) {
                orcRes = paddle.ocrText(clip);
                clip.recycle();
                log("坐标截取", orcRes)
                let xy = "";
                if (orcRes.length) {
                    xy = orcRes[0];
                } else {
                    alert('资源坐标失败,先打开资源！')
                    toastLog('资源坐标失败,先打开资源！')
                    return
                }
                if (isNaN(xy)) {
                    if (xy.includes('(') || xy.includes('（')) {
                        xy = xy.substring(1, xy.length)
                    }
                }
                let xy3 = xy[xy.length]
                if (isNaN(xy3)) {
                    if (xy.includes(')') || xy.includes('）')) {
                        xy = xy.substring(0, xy.length - 1)
                    }
                }
                xy = xy.replace("口", "0");
                xy = xy.replace("日", "8");
                xy = xy.replace(/\s+/g, "");

                let douhao = ',';
                if (xy.includes('，')) {
                    douhao = '，';
                }
                if (xy.includes('.')) {
                    douhao = '.';
                }
                let xyArr = xy.split(douhao)
                if (isNaN(xyArr[0]) || isNaN(xyArr[1])) {
                    // 百度失败再用谷歌
                    clip = images.clip(captureScreen(), ziyuanZuobianWeizhi.x - 80, ziyuanZuobianWeizhi.y, (400 * picJizhun), (220 * picJizhunHeight));
                    orcRes = gmlkit.ocr(clip, "zh");
                    orcRes = orcRes.text;
                    clip.recycle();
                    log("坐标截取", orcRes)
                    let xy = "";
                    if (orcRes && orcRes != "" && orcRes != "undefined") {
                        xy = orcRes;
                    } else {
                        alert('资源坐标失败,先打开资源！')
                        toastLog('资源坐标失败,先打开资源！')
                        return
                    }
                    if (isNaN(xy)) {
                        if (xy.includes('(') || xy.includes('（')) {
                            xy = xy.substring(1, xy.length)
                        }
                    }
                    let xy3 = xy[xy.length]
                    if (isNaN(xy3)) {
                        if (xy.includes(')') || xy.includes('）')) {
                            let index = "";
                            if (xy.includes(')')) {
                                index = xy.indexOf(")");
                            }
                            if (xy.includes('）')) {
                                index = xy.indexOf("）");
                            }
                            xy = xy.substring(0, index)
                        }
                    }
                    xy = xy.replace("口", "0");
                    xy = xy.replace("日", "8");
                    xy = xy.replace(/\s+/g, "");

                    let douhao = ',';
                    if (xy.includes('，')) {
                        douhao = '，';
                    }
                    if (xy.includes('.')) {
                        douhao = '.';
                    }
                    let xyArr = xy.split(douhao)
                    if (isNaN(xyArr[0]) || isNaN(xyArr[1])) {
                        toastLog('读取失败，请手动添加')
                        alert('读取失败，请手动添加')
                    } else {
                        log('坐标 X:' + xyArr[0] + ' Y:' + xyArr[1])
                        let type = 0
                        if (orcRes.includes('田')) {
                            type = 0
                        }
                        if (orcRes.includes('木')) {
                            type = 1
                        }
                        if (orcRes.includes('矿')) {
                            type = 2
                        }
                        for (let index = 0; index < toCaijiList.length; index++) {
                            let element = toCaijiList[index];
                            if (element.x == xyArr[0] && element.y == xyArr[1]) {
                                alert("已添加过该资源")
                                return;
                            }
                        }
                        let level = 1;
                        let clip = images.clip(captureScreen(), (deviceWidth / 2), 0, (deviceWidth / 2), (deviceHeight / 2));
                        let orcResJi = gmlkit.ocr(clip, "zh");
                            clip.recycle();
                            log('jijijgmlkit',orcResJi)
                            level = findMatch(orcResJi.text)
                            log('jijigmlkit',level)


                        toCaijiList.push({
                            x: xyArr[0],
                            y: xyArr[1],
                            level: Number(level),
                            type: type
                        });
                        let tepps = ['田', '木', '铁']
                        alert('X:' + xyArr[0] + ' Y:' + xyArr[1] + ' ' + level + '级 ' + tepps[type] + ' 已添加');
                        toastLog('X:' + xyArr[0] + ' Y:' + xyArr[1] + ' 已添加')
                        log("添加完成")
                        storage.put("caijiItems", toCaijiList);
                    }
                } else {
                    log('坐标 X:' + xyArr[0] + ' Y:' + xyArr[1])
                    let type = 0
                    for (let index = 0; index < orcRes.length; index++) {
                        let element = orcRes[index];
                        if (element.includes('田')) {
                            type = 0
                        }
                        if (element.includes('木')) {
                            type = 1
                        }
                        if (element.includes('矿')) {
                            type = 2
                        }
                    }
                    for (let index = 0; index < toCaijiList.length; index++) {
                        let element = toCaijiList[index];
                        if (element.x == xyArr[0] && element.y == xyArr[1]) {
                            alert("已添加过该资源")
                            return;
                        }
                    }
                    let level = 1;
                    let clip = images.clip(captureScreen(), (deviceWidth / 2), 0, (deviceWidth / 2), (deviceHeight / 2));
                    let orcResJi = paddle.ocrText(clip);
                        clip.recycle();
                        log('jijijijijiorcRes',orcResJi)
                        level = findMatch(orcResJi)
                        log('jijijijijiorcRes',level)

                        toCaijiList.push({
                            x: xyArr[0],
                            y: xyArr[1],
                            level: Number(level),
                            type: type
                        });

                    let tepps = ['田', '木', '铁']
                    alert('X:' + xyArr[0] + ' Y:' + xyArr[1] + ' ' + level + '级 ' + tepps[type] + ' 已添加');
                    toastLog('X:' + xyArr[0] + ' Y:' + xyArr[1] + ' 已添加')
                    log("添加完成")
                    storage.put("caijiItems", toCaijiList);
                }

            } else if (parseInt(storage.get("shibiefangshi", 0)) == 1) {
                orcRes = paddle.ocrText(clip);
                clip.recycle();
                log("坐标截取", orcRes)
                let xy = "";
                if (orcRes.length) {
                    xy = orcRes[0];
                } else {
                    alert('资源坐标失败,先打开资源！')
                    toastLog('资源坐标失败,先打开资源！')
                    return
                }
                if (isNaN(xy)) {
                    if (xy.includes('(') || xy.includes('（')) {
                        xy = xy.substring(1, xy.length)
                    }
                }
                let xy3 = xy[xy.length]
                if (isNaN(xy3)) {
                    if (xy.includes(')') || xy.includes('）')) {
                        xy = xy.substring(0, xy.length - 1)
                    }
                }
                xy = xy.replace("口", "0");
                xy = xy.replace("日", "8");
                xy = xy.replace(/\s+/g, "");

                let douhao = ',';
                if (xy.includes('，')) {
                    douhao = '，';
                }
                if (xy.includes('.')) {
                    douhao = '.';
                }
                let xyArr = xy.split(douhao)
                if (isNaN(xyArr[0]) || isNaN(xyArr[1])) {
                    toastLog('读取失败，请手动添加')
                } else {
                    log('坐标 X:' + xyArr[0] + ' Y:' + xyArr[1])
                    let type = 0
                    for (let index = 0; index < orcRes.length; index++) {
                        let element = orcRes[index];
                        if (element.includes('田')) {
                            type = 0
                        }
                        if (element.includes('木')) {
                            type = 1
                        }
                        if (element.includes('矿')) {
                            type = 2
                        }
                    }
                    for (let index = 0; index < toCaijiList.length; index++) {
                        let element = toCaijiList[index];
                        if (element.x == xyArr[0] && element.y == xyArr[1]) {
                            alert("已添加过该资源")
                            return;
                        }
                    }
                    let level = 1;
                    let clip = images.clip(captureScreen(), (deviceWidth / 2), 0, (deviceWidth / 2), (deviceHeight / 2));
                    let orcResJi = paddle.ocrText(clip);
                        clip.recycle();
                        log('jijijijijiorcRes',orcResJi)
                        level = findMatch(orcResJi)
                        log('jijijijijiorcRes',level)

                        toCaijiList.push({
                            x: xyArr[0],
                            y: xyArr[1],
                            level: Number(level),
                            type: type
                        });

                    let tepps = ['田', '木', '铁']
                    alert('X:' + xyArr[0] + ' Y:' + xyArr[1] + ' ' + level + '级 ' + tepps[type] + ' 已添加');
                    toastLog('X:' + xyArr[0] + ' Y:' + xyArr[1] + ' 已添加')
                    log("添加完成")
                    storage.put("caijiItems", toCaijiList);
                }
            } else {
                // 纯用谷歌
                orcRes = gmlkit.ocr(clip, "zh");
                orcRes = orcRes.text;
                clip.recycle();
                log("坐标截取", orcRes)

                let xy = "";
                if (orcRes && orcRes != "" && orcRes != "undefined") {
                    xy = orcRes;
                } else {
                    alert('资源坐标失败,先打开资源！')
                    toastLog('资源坐标失败,先打开资源！')
                    return
                }
                if (isNaN(xy)) {
                    if (xy.includes('(') || xy.includes('（')) {
                        xy = xy.substring(1, xy.length)
                    }
                }
                let xy3 = xy[xy.length]
                if (isNaN(xy3)) {
                    if (xy.includes(')') || xy.includes('）')) {
                        let index = "";
                        if (xy.includes(')')) {
                            index = xy.indexOf(")");
                        }
                        if (xy.includes('）')) {
                            index = xy.indexOf("）");
                        }
                        xy = xy.substring(0, index)
                    }
                }
                xy = xy.replace("口", "0");
                xy = xy.replace("日", "8");
                xy = xy.replace(/\s+/g, "");

                let douhao = ',';
                if (xy.includes('，')) {
                    douhao = '，';
                }
                if (xy.includes('.')) {
                    douhao = '.';
                }
                let xyArr = xy.split(douhao)
                if (isNaN(xyArr[0]) || isNaN(xyArr[1])) {
                    toastLog('读取失败，请手动添加')
                } else {
                    log('坐标 X:' + xyArr[0] + ' Y:' + xyArr[1])
                    let type = 0
                    if (orcRes.includes('田')) {
                        type = 0
                    }
                    if (orcRes.includes('木')) {
                        type = 1
                    }
                    if (orcRes.includes('矿')) {
                        type = 2
                    }
                    for (let index = 0; index < toCaijiList.length; index++) {
                        let element = toCaijiList[index];
                        if (element.x == xyArr[0] && element.y == xyArr[1]) {
                            alert("已添加过该资源")
                            return;
                        }
                    }
                    let level = 1;
                        let clip = images.clip(captureScreen(), (deviceWidth / 2), 0, (deviceWidth / 2), (deviceHeight / 2));
                        let orcResJi = gmlkit.ocr(clip, "zh");
                            clip.recycle();
                            log('jijijgmlkit',orcResJi)
                            level = findMatch(orcResJi.text)
                            log('jijigmlkit',level)


                        toCaijiList.push({
                            x: xyArr[0],
                            y: xyArr[1],
                            level: Number(level),
                            type: type
                        });
                    let tepps = ['田', '木', '铁']
                    alert('X:' + xyArr[0] + ' Y:' + xyArr[1] + ' ' + level + '级 ' + tepps[type] + ' 已添加');
                    toastLog('X:' + xyArr[0] + ' Y:' + xyArr[1] + ' 已添加')
                    log("添加完成")
                    storage.put("caijiItems", toCaijiList);
                }

            }

        })
    })

    if (bloo) {
        floatyWindow.setPosition(188, 12);
    } else {
        floatyWindow.setPosition(168, 12);
    }
    floatyWindow.peizhishezhi.setVisibility(8);
    floatyWindow.ziyuanpeizhi.setVisibility(8);
    floatyWindow.myExit.setVisibility(8);
    setBackgroundRoundRounded(floatyWindow.floatyAction, Color.GREEN);
}

// 设置按钮样式
function setBackgroundRoundRounded(view, bloo) {
    gradientDrawable = new GradientDrawable();
    gradientDrawable.setShape(GradientDrawable.RECTANGLE);
    gradientDrawable.setColor(bloo);
    gradientDrawable.setCornerRadius(80);
    view.setBackground(gradientDrawable);
}

// 悬浮窗按钮点击
function floatyActionOnClick() {
    if (floatyWindow.floatyAction.getText() == '运') {
        setBackgroundRoundRounded(floatyWindow.floatyAction, Color.RED);
        floatyWindow.peizhishezhi.setVisibility(0);
        floatyWindow.ziyuanpeizhi.setVisibility(0);
        floatyWindow.myExit.setVisibility(0);
        floatyWindow.floatyAction.setText('停');
        //向该脚本发送事件
        storage.put("zanting", true);
        toastLog("停止");
    } else if (floatyWindow.floatyAction.getText() == '停') {
        floatyWindow.peizhishezhi.setVisibility(8);
        floatyWindow.ziyuanpeizhi.setVisibility(8);
        floatyWindow.myExit.setVisibility(8);
        setBackgroundRoundRounded(floatyWindow.floatyAction, Color.GREEN);
        storage.put("zanting", false);
        floatyWindow.floatyAction.setText('运');
        toastLog("运行")
    } else {
        myLogFunc("？？？...")
    }
}

// 监听本脚本运行结束
events.on("exit", function () {
    floaty.closeAll()
    threads.shutDownAll()
    engines.stopAll()
});

